﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FF14TreasureDiggingTool.DataAccess.ApplicationDbContext;
using FF14TreasureDiggingTool.DataAccess.Models;
using FF14TreasureDiggingTool.IOC;
using Microsoft.Win32;
using Syncfusion.Data.Extensions;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Converter;
using Syncfusion.XlsIO;
using Wpf.Ui.Controls.Interfaces;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.Mvvm.Services;

namespace FF14TreasureDiggingTool.ViewModels;

public partial class TaskManagerViewModel : ObservableObject
{
    private readonly ApplicationContext _dbContext;
    private bool _dataInitialized = false;

    [ObservableProperty] private List<int> _comboBoxitem;
    [ObservableProperty] private DungeonsRecord _selectedRecord;
    private ObservableCollection<DungeonsRecord> _dungeonsRecords;

    public ObservableCollection<DungeonsRecord> DungeonsRecords
    {
        get => _dungeonsRecords;
        set => SetProperty(ref _dungeonsRecords, value);
    }
    public TaskManagerViewModel(ApplicationContext dbContext)
    {
        _dbContext = dbContext;

        if (!_dataInitialized)
            InitializeViewModel();
    }

    private void InitializeViewModel()
    {
        DungeonsRecords = new ObservableCollection<DungeonsRecord>(_dbContext.DungeonsRecords.ToList<DungeonsRecord>());
        ComboBoxitem = new List<int> {5,25,50,100,250 };
        SelectedRecord = new DungeonsRecord();
        _dataInitialized = true;
    }

    [RelayCommand]
    public void ExportToFile(SfDataGrid dataGrid)
    {
        var options = new ExcelExportingOptions();
        options.ExcelVersion = ExcelVersion.Excel2013;
        var excelEngine = dataGrid.ExportToExcel(dataGrid.View, options);
        var workBook = excelEngine.Excel.Workbooks[0];

        SaveFileDialog sfd = new SaveFileDialog
        {
            FilterIndex = 2,
            Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx"
        };

        if (sfd.ShowDialog() == true)
        {
            using (Stream stream = sfd.OpenFile())
            {

                if (sfd.FilterIndex == 1)
                    workBook.Version = ExcelVersion.Excel97to2003;

                else if (sfd.FilterIndex == 2)
                    workBook.Version = ExcelVersion.Excel2010;

                else
                    workBook.Version = ExcelVersion.Excel2013;
                workBook.SaveAs(stream);
            }
            
        }
    }

    [RelayCommand]
    public async void RemoveRecord()
    {
        var _snackbarControl = IocManager.Resolve<ISnackbarService>().GetSnackbarControl();
        
        if (SelectedRecord is null)
        {
            _snackbarControl.Show("请先选中任意一行再点击删除按钮");
            return;
        }

        if (string.IsNullOrEmpty(SelectedRecord.RuleName))
        {
            _snackbarControl.Show("请先选中任意一行再点击删除按钮");
            return;
        }

        var _dialogControl = IocManager.Resolve<IDialogService>().GetDialogControl();
        _dialogControl.ButtonLeftClick += DialogControlButtonRightClick;
        _dialogControl.ButtonRightClick += DialogControlButtonRightClick;
        var button = await _dialogControl.ShowAndWaitAsync("注意", "是否删除该记录？", true);
        if (button==IDialogControl.ButtonPressed.Left)
        {
            return;
        }


        _dbContext.DungeonsRecords.Remove(SelectedRecord);
        _dbContext.SaveChanges();

        DungeonsRecords = new ObservableCollection<DungeonsRecord>(_dbContext.DungeonsRecords.ToList<DungeonsRecord>());
    }

    [RelayCommand]
    public void Refresh()
    {
        DungeonsRecords = new ObservableCollection<DungeonsRecord>(_dbContext.DungeonsRecords.ToList<DungeonsRecord>());
    }

    private static void DialogControlButtonRightClick(object sender, RoutedEventArgs e)
    {
        var dialogControl = (IDialogControl)sender;
        dialogControl.Hide();
    }
}
